<?php

namespace App\Libs\Strategy;


use Illuminate\Database\Eloquent\Model;

class DeleteData implements IStrategy
{


    private $model;

    private $id;

    private $callable;

    public $deleteTrue = false;

    public $attrbute;

    public function __construct(Model $model,$id,$callable)
    {
        $this->model = $model;
        $this->id = $id;
        $this->callable = $callable;
    }


    public function algorithm()
    {
        $this->attrbute = $this->model->find($this->id);
        if(is_callable($this->callable))
        {
            $this->deleteTrue = call_user_func($this->callable,$this);
        }
        if($this->deleteTrue)
        {
            //删除真实数据
            return $this->attrbute->forceDelete();
        }
        if(in_array('deleted_at',$this->attrbute->getDates())){
            $this->attrbute->delete();
            return $this->attrbute->trashed();
        }else{
            return $this->attrbute->delete();
        }
    }
}
